package a11_单调栈;

import java.util.Arrays;
import java.util.Stack;

/**
 * <p>
 * a03_下一个更大元素2复习1
 * </p>
 *
 * @author flyduck
 * @since 2025/3/26
 */
public class a03_下一个更大元素2复习1 {
    public int[] nextGreaterElements(int[] nums) {
        int[] result = new int[nums.length];
        Arrays.fill(result, -1);
        Stack<Integer> stack = new Stack<>();
        stack.push(0);

        //length=4
        //0 1 2 3
        //4 5 6 7
        for (int i = 0; i < nums.length * 2; i++) {
            while (!stack.isEmpty() && nums[stack.peek()] < nums[i % nums.length]){
                Integer pop = stack.pop();
                result[pop] = nums[i % nums.length];
            }
            if(i < nums.length){
                stack.push(i);
            }
        }

        return result;
    }
}
